package ru.smc.hogwarts.servlets.rooms;

import java.io.IOException;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import ru.smc.hogwarts.classes.EMF;
import ru.smc.hogwarts.model.Room;

public class RoomsList extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private HttpSession session;
       
    public RoomsList() {
        super();
    }

	@SuppressWarnings("unchecked")
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		session = request.getSession(true);
    	session.setAttribute("servletName", getServletName());
    	
    	EntityManagerFactory emf = EMF.getInstance().getEMF();
    	EntityManager em = emf.createEntityManager();
    	
    	List<Room> rooms = em.createQuery("select p from Room p").getResultList();
		request.setAttribute("rooms", rooms);
		
		em.close();
		
		request.getRequestDispatcher("/pages/rooms/roomsList.jsp").forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}

}
